package com.laiketui.common.service.dubbo.ocr;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.laiketui.common.api.ocr.OcrService;
import com.laiketui.domain.lktconst.DictionaryConst;
import com.laiketui.domain.ocr.BusinessLicenseRoot;
import com.laiketui.domain.ocr.IdCardRoot;
import org.springframework.stereotype.Service;

@Service
public class OcrServiceImpl implements OcrService {

    public Client createClient() throws Exception{
        Config config = new Config()
                .setAccessKeyId("LTAI5tS5hTK2DiiXJJW4kAEB")
                .setAccessKeySecret("jS9YRK4eUyrseNT8crMJt3kT9E0P2t");
        config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com";
        return new Client(config);
    }

    @Override
    public Object getOcrInfo(int ocrType,String imgPath) throws Exception {
        Object o = null;
        Client client = this.createClient();
        String data = "";
        if(ocrType == DictionaryConst.OcrType.OCR_TYPE_IDCARD){
            RecognizeIdcardRequest idcardRequest = new RecognizeIdcardRequest()
                    .setUrl(imgPath);
            RecognizeIdcardResponse idcardResponse = client.recognizeIdcard(idcardRequest);
            RecognizeIdcardResponseBody body = idcardResponse.getBody();
            data = body.getData();
            IdCardRoot idCardRoot = JSONObject.parseObject(data, IdCardRoot.class);
            o = idCardRoot;
        }
        if(ocrType == DictionaryConst.OcrType.OCR_TYPE_LICENSE){
            RecognizeBusinessLicenseRequest businessLicenseRequest = new RecognizeBusinessLicenseRequest()
                    .setUrl(imgPath);
            RecognizeBusinessLicenseResponse licenseResponse = client.recognizeBusinessLicense(businessLicenseRequest);
            RecognizeBusinessLicenseResponseBody body = licenseResponse.getBody();
            data = body.getData();
            BusinessLicenseRoot licenseRoot = JSONObject.parseObject(data, BusinessLicenseRoot.class);
            o = licenseRoot;
        }
        return o;

    }
}
